settings.ts ➔ restoreOptions   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import { getGlobalStorageProvider } from "./browserApi/storageProvider";
2
import { onReady } from "./utils/helpers";
3
4
const OPTION_SELECTOR = 'input[type="checkbox"';
5
6
function storeOptions() {
7
    document.querySelectorAll(OPTION_SELECTOR).forEach(optionElement => {
8
        let optionInputElement = optionElement as HTMLInputElement;
9
        getGlobalStorageProvider().setDataAsBoolean(optionInputElement.id, optionInputElement.checked);
10
    });
11
}
12
13
function restoreOptions() {
14
    document.querySelectorAll(OPTION_SELECTOR).forEach(optionElement => {        
15
        let optionInputElement = optionElement as HTMLInputElement;
16
        let defaultValue = optionInputElement.dataset.defaultValue === 'true' ? true : false;
17
18
        getGlobalStorageProvider().getDataAsBoolean(optionInputElement.id, defaultValue, value => {
19
            optionInputElement.checked = value;
20
        });
21
    });
22
}
23
24
function resetOptions() {
25
    document.querySelectorAll(OPTION_SELECTOR).forEach(optionElement => {
26
        let optionInputElement = optionElement as HTMLInputElement;
27
        let defaultValue = optionInputElement.dataset.defaultValue === 'true' ? true : false;
28
29
        optionInputElement.checked = defaultValue;
30
    });
31
}
32
33
onReady(() => {
34
    // register Store Button
35
    document.getElementById('btnSave').addEventListener('click', event => {
36
        event.preventDefault();
37
        storeOptions();
38
    });
39
40
    document.getElementById('btnReset').addEventListener('click', event => {
41
        event.preventDefault();
42
        resetOptions();
43
        storeOptions();
44
    })
45
46
    // try restore options
47
    restoreOptions();
48
49
    // update version label
50
    document.getElementById('version').innerText = `v${chrome.runtime.getManifest().version}`
51
});